//
//  FFRedPacketsSystemContent.m
//  LiMaoRedPackets
//
//  Created by tt on 2020/9/22.
//

#import "FFRedPacketsSystemContent.h"

@interface FFRedPacketsSystemContent ()

@property(nonatomic,strong) NSArray *contentParam;

@property(nonatomic,copy) NSString *content; // 红包内容

@property(nonatomic,assign) CGSize displayContent2SizeInner;

@property(nonatomic,strong) NSDictionary *dic;
@end

@implementation FFRedPacketsSystemContent


- (void)decodeWithJSON:(NSDictionary *)contentDic {
    self.dic = contentDic;
     self.content = contentDic[@"content"];
    self.contentParam = contentDic[@"extra"];
    self.redpacketNo = contentDic[@"redpacket_no"];
    self.displayContent =[self getDisplayContent];
}

+ (NSNumber *)contentType {
    return  @(WK_REDPACKET_OPEN);
}

- (NSString *)conversationDigest {
    return self.displayContent;
}

- (NSString *)searchableWord {
    return self.displayContent;
}


-(NSString*) getDisplayContent {
    if(!self.content) {
        return @"未知";
    }
    NSString *content =self.content;
    NSArray *extraArray = self.contentParam;
    NSString *type = self.dic[@"notice"];
    if ([type isKindOfClass:[NSString class]] && [type isEqualToString:@"转账退款"]){
        if(extraArray.count>0) {
            for (int i=0; i<=extraArray.count-1; i++) {
                NSDictionary *extrDict = extraArray[i];
                NSString *name = extrDict[@"name"]?:@"";
                
                if([[WKSDK shared].options.connectInfo.uid isEqualToString:extrDict[@"uid"]]) {
                    name = @"";
                }
                content = [content stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"{%d}",i] withString:@"对方"];
            }
        }
        return content;
    }
    if(extraArray.count>0) {
        for (int i=0; i<=extraArray.count-1; i++) {
            NSDictionary *extrDict = extraArray[i];
            NSString *name = extrDict[@"name"]?:@"";
            
            if([[WKSDK shared].options.connectInfo.uid isEqualToString:extrDict[@"uid"]]) {
                name = LLang(@"你");
            }
            content = [content stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"{%d}",i] withString:name];
        }
    }
    return content;
}

- (NSAttributedString *)displayContent2 {
    if(!_displayContent2) {
        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self.displayContent attributes:@{
            NSFontAttributeName: [[WKApp shared].config appFontOfSize:[WKApp shared].config.messageTipTimeFontSize],
        }];

        NSString *type = self.dic[@"notice"];
        if ([type isKindOfClass:[NSString class]] && [type isEqualToString:@"转账退款"]){
            _displayContent2 = attrStr;
            return _displayContent2;
        }
        
        NSString *redpacketTitle = @"红包";
        
        [attrStr addAttributes:@{
//            NSLinkAttributeName: @"chat",
            NSForegroundColorAttributeName: [UIColor redColor],
            NSUnderlineStyleAttributeName: @(0),
            NSFontAttributeName: [[WKApp shared].config appFontOfSize:[WKApp shared].config.messageTipTimeFontSize],
        } range:NSMakeRange(attrStr.length - redpacketTitle.length,redpacketTitle.length )];
        
        NSTextAttachment *imageAtta = [[NSTextAttachment alloc] init];
        imageAtta.bounds = CGRectMake(0, -4.0f, 16.0f, 16.0f);
        imageAtta.image = [self imageName:@"redpacket_smallicon"];
        [attrStr insertAttributedString:[NSAttributedString attributedStringWithAttachment:imageAtta] atIndex:0];
       
//        [attrStr lim_parse:<#(nonnull NSString *)#>];
        
        _displayContent2 = attrStr;
        
      
    }
    return _displayContent2;
}


-(UIImage*) imageName:(NSString*)name {

    return [[WKApp shared] loadImage:name moduleID:@"LiMaoRedPackets"];
}

-(CGSize) displayContent2Size:(CGFloat)maxWidth {
    if(!CGSizeEqualToSize(self.displayContent2SizeInner, CGSizeZero)) {
        return self.displayContent2SizeInner;
    }
    CGSize size =   [self.displayContent2 boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    self.displayContent2SizeInner = size;
    return self.displayContent2SizeInner;
}

@end
